<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.detailId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      
      <el-form-item label="收货单" prop="deliverNo">
        <el-input v-model="dataForm.deliverNo" disabled placeholder="收货单"></el-input>
      </el-form-item>
      <el-form-item label="器材代码" prop="spuNo">
        <el-select v-model="dataForm.spuNo" @change="selectChange" filterable placeholder="请选择器材代码">
          <el-option v-for="(item,index) in spuOptions" :key="index" :label="item.spuNo" :value="item.spuNo">
            <span style="float: left">{{ item.spuNo }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.spuName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收货数量" prop="planQty">
        <el-input v-model="dataForm.planQty" placeholder="收货数量"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input v-model="dataForm.comments" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        detailId: '',
        purchaseId: '',
        deliverNo:'',
        spuNo: '',
        spuName: '',
        planQty: '',
        comments: ''
      },
      total: 0,
      optionsParams: {
        spuNo: '',
        page: 1,
        limit: 20000
      },
      spuOptions: []
    }
  },
  created() {
    this.queryOptions()
  },
  watch: {
    visible(val) {
      if (val) {
        this.optionsParams.page = 1
        this.queryOptions()
      }
    }
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        planQty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 器材名称
    selectChange(e) {
      this.spuOptions.find((item) => {
        if(item.spuNo==e){
          this.dataForm.spuName=item.spuName;
        }
      });
    },
    pageSizeChangeHandle(val) {
      this.optionsParams.page = 1
      this.optionsParams.limit = val
      this.queryOptions()
    },
    // 分页, 当前页
    pageCurrentChangeHandle(val) {
      this.optionsParams.page = val
      this.queryOptions()
    },
    queryOptions() {
      this.$http.get('/wms/spu/page', {params: this.optionsParams}).then(({ data: res }) => {
        if (res.code !== 0) {
          this.dataLspuOptionsist = []
          this.total = 0
          return this.$message.error(res.msg)
        }
        this.spuOptions = res.data.list
        this.total = res.data.total
      })
    },
    init() {
      this.visible = true
      this.dataForm.detailId = this.dataForm.id
      this.dataForm.purchaseId = this.$route.params.purchaseId
      this.dataForm.deliverNo=this.$route.params.deliverNo
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.detailId) {
          this.getInfo()
        } else {
          this.dataForm.createDate = null
          this.dataForm.creator = null
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/wms/purchase-detail/${this.dataForm.detailId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data,
          updateDate: null,
          updater: null
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.detailId ? 'post' : 'put']('/wms/purchase-detail/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
.el-select{
  width: 100%;
}
.el-pagination {
  display: flex;
  justify-content: center;
}
</style>
